<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="68px"
    >
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
        <el-button
          :icon="!isOpen ? 'el-icon-arrow-down' : 'el-icon-arrow-up'"
          size="mini"
          @click="shouqi"
          >展开/收起</el-button
        >
      </el-form-item>

      <div v-if="isOpen">
        <el-form-item label="请求编码" prop="requestCode">
          <el-input
            v-model="queryParams.requestCode"
            placeholder="请输入请求编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售金额" prop="salesAmount">
          <el-input
            v-model="queryParams.salesAmount"
            placeholder="请输入销售金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售金额环比" prop="salesAmountChain">
          <el-input
            v-model="queryParams.salesAmountChain"
            placeholder="请输入销售金额环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售金额同比" prop="salesAmountYoy">
          <el-input
            v-model="queryParams.salesAmountYoy"
            placeholder="请输入销售金额同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退货件数" prop="returnNumber">
          <el-input
            v-model="queryParams.returnNumber"
            placeholder="请输入退货件数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退货件数环比" prop="returnNumberChain">
          <el-input
            v-model="queryParams.returnNumberChain"
            placeholder="请输入退货件数环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退货件数同比" prop="returnNumberYoy">
          <el-input
            v-model="queryParams.returnNumberYoy"
            placeholder="请输入退货件数同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退款金额" prop="returnAmount">
          <el-input
            v-model="queryParams.returnAmount"
            placeholder="请输入退款金额"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退款金额环比" prop="returnAmountChain">
          <el-input
            v-model="queryParams.returnAmountChain"
            placeholder="请输入退款金额环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="退款金额同比" prop="returnAmountYoy">
          <el-input
            v-model="queryParams.returnAmountYoy"
            placeholder="请输入退款金额同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售件数" prop="salesNumber">
          <el-input
            v-model="queryParams.salesNumber"
            placeholder="请输入销售件数"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售件数环比" prop="salesNumberChain">
          <el-input
            v-model="queryParams.salesNumberChain"
            placeholder="请输入销售件数环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="销售件数同比" prop="salesNumberYoy">
          <el-input
            v-model="queryParams.salesNumberYoy"
            placeholder="请输入销售件数同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="综合退货率" prop="comprehensiveReturnRate">
          <el-input
            v-model="queryParams.comprehensiveReturnRate"
            placeholder="请输入综合退货率"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label="综合退货率环比"
          prop="comprehensiveReturnRateChain"
        >
          <el-input
            v-model="queryParams.comprehensiveReturnRateChain"
            placeholder="请输入综合退货率环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="综合退货率同比" prop="comprehensiveReturnRateYoy">
          <el-input
            v-model="queryParams.comprehensiveReturnRateYoy"
            placeholder="请输入综合退货率同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="折扣率" prop="discountRate">
          <el-input
            v-model="queryParams.discountRate"
            placeholder="请输入折扣率"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="折扣率环比" prop="discountRateChain">
          <el-input
            v-model="queryParams.discountRateChain"
            placeholder="请输入折扣率环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="折扣率同比" prop="discountRateYoy">
          <el-input
            v-model="queryParams.discountRateYoy"
            placeholder="请输入折扣率同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="主推款销售额占比" prop="operationMainAmountRatio">
          <el-input
            v-model="queryParams.operationMainAmountRatio"
            placeholder="请输入主推款销售额占比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label="主推款销售额占比环比"
          prop="operationMainAmountRatioChain"
        >
          <el-input
            v-model="queryParams.operationMainAmountRatioChain"
            placeholder="请输入主推款销售额占比环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item
          label="主推款销售额占比同比"
          prop="operationMainAmountRatioYoy"
        >
          <el-input
            v-model="queryParams.operationMainAmountRatioYoy"
            placeholder="请输入主推款销售额占比同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="亏损销售额占比" prop="lossAmountRatio">
          <el-input
            v-model="queryParams.lossAmountRatio"
            placeholder="请输入亏损销售额占比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="亏损销售额占比环比" prop="lossAmountRatioChain">
          <el-input
            v-model="queryParams.lossAmountRatioChain"
            placeholder="请输入亏损销售额占比环比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="亏损销售额占比同比" prop="lossAmountRatioYoy">
          <el-input
            v-model="queryParams.lossAmountRatioYoy"
            placeholder="请输入亏损销售额占比同比"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
      </div>
      <!-- <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item> -->
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['sale:BiMonitoringMiddleDwd:add']"
          >新增</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['sale:BiMonitoringMiddleDwd:edit']"
          >修改</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['sale:BiMonitoringMiddleDwd:remove']"
          >删除</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['sale:BiMonitoringMiddleDwd:export']"
          >导出</el-button
        >
      </el-col>
      <el-col :span="1.5">
        <el-button size="mini" @click="handlesynData">同步</el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="BiMonitoringMiddleDwdList"
      @selection-change="handleSelectionChange"
      :height="600"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="ID" align="center" prop="id" />
      <el-table-column label="请求编码" align="center" prop="requestCode" />
      <el-table-column label="销售金额" align="center" prop="salesAmount" />
      <el-table-column
        label="销售金额环比"
        align="center"
        prop="salesAmountChain"
      />
      <el-table-column
        label="销售金额同比"
        align="center"
        prop="salesAmountYoy"
      />
      <el-table-column label="退货件数" align="center" prop="returnNumber" />
      <el-table-column
        label="退货件数环比"
        align="center"
        prop="returnNumberChain"
      />
      <el-table-column
        label="退货件数同比"
        align="center"
        prop="returnNumberYoy"
      />
      <el-table-column label="退款金额" align="center" prop="returnAmount" />
      <el-table-column
        label="退款金额环比"
        align="center"
        prop="returnAmountChain"
      />
      <el-table-column
        label="退款金额同比"
        align="center"
        prop="returnAmountYoy"
      />
      <el-table-column label="销售件数" align="center" prop="salesNumber" />
      <el-table-column
        label="销售件数环比"
        align="center"
        prop="salesNumberChain"
      />
      <el-table-column
        label="销售件数同比"
        align="center"
        prop="salesNumberYoy"
      />
      <el-table-column
        label="综合退货率"
        align="center"
        prop="comprehensiveReturnRate"
      />
      <el-table-column
        label="综合退货率环比"
        align="center"
        prop="comprehensiveReturnRateChain"
      />
      <el-table-column
        label="综合退货率同比"
        align="center"
        prop="comprehensiveReturnRateYoy"
      />
      <el-table-column label="折扣率" align="center" prop="discountRate" />
      <el-table-column
        label="折扣率环比"
        align="center"
        prop="discountRateChain"
      />
      <el-table-column
        label="折扣率同比"
        align="center"
        prop="discountRateYoy"
      />
      <el-table-column
        label="主推款销售额占比"
        align="center"
        prop="operationMainAmountRatio"
      />
      <el-table-column
        label="主推款销售额占比环比"
        align="center"
        prop="operationMainAmountRatioChain"
      />
      <el-table-column
        label="主推款销售额占比同比"
        align="center"
        prop="operationMainAmountRatioYoy"
      />
      <el-table-column
        label="亏损销售额占比"
        align="center"
        prop="lossAmountRatio"
      />
      <el-table-column
        label="亏损销售额占比环比"
        align="center"
        prop="lossAmountRatioChain"
      />
      <el-table-column
        label="亏损销售额占比同比"
        align="center"
        prop="lossAmountRatioYoy"
      />
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['sale:BiMonitoringMiddleDwd:edit']"
            >修改</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['sale:BiMonitoringMiddleDwd:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品监控缓存对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="请求编码" prop="requestCode">
          <el-input v-model="form.requestCode" placeholder="请输入请求编码" />
        </el-form-item>
        <el-form-item label="销售金额" prop="salesAmount">
          <el-input v-model="form.salesAmount" placeholder="请输入销售金额" />
        </el-form-item>
        <el-form-item label="销售金额环比" prop="salesAmountChain">
          <el-input
            v-model="form.salesAmountChain"
            placeholder="请输入销售金额环比"
          />
        </el-form-item>
        <el-form-item label="销售金额同比" prop="salesAmountYoy">
          <el-input
            v-model="form.salesAmountYoy"
            placeholder="请输入销售金额同比"
          />
        </el-form-item>
        <el-form-item label="退货件数" prop="returnNumber">
          <el-input v-model="form.returnNumber" placeholder="请输入退货件数" />
        </el-form-item>
        <el-form-item label="退货件数环比" prop="returnNumberChain">
          <el-input
            v-model="form.returnNumberChain"
            placeholder="请输入退货件数环比"
          />
        </el-form-item>
        <el-form-item label="退货件数同比" prop="returnNumberYoy">
          <el-input
            v-model="form.returnNumberYoy"
            placeholder="请输入退货件数同比"
          />
        </el-form-item>
        <el-form-item label="退款金额" prop="returnAmount">
          <el-input v-model="form.returnAmount" placeholder="请输入退款金额" />
        </el-form-item>
        <el-form-item label="退款金额环比" prop="returnAmountChain">
          <el-input
            v-model="form.returnAmountChain"
            placeholder="请输入退款金额环比"
          />
        </el-form-item>
        <el-form-item label="退款金额同比" prop="returnAmountYoy">
          <el-input
            v-model="form.returnAmountYoy"
            placeholder="请输入退款金额同比"
          />
        </el-form-item>
        <el-form-item label="销售件数" prop="salesNumber">
          <el-input v-model="form.salesNumber" placeholder="请输入销售件数" />
        </el-form-item>
        <el-form-item label="销售件数环比" prop="salesNumberChain">
          <el-input
            v-model="form.salesNumberChain"
            placeholder="请输入销售件数环比"
          />
        </el-form-item>
        <el-form-item label="销售件数同比" prop="salesNumberYoy">
          <el-input
            v-model="form.salesNumberYoy"
            placeholder="请输入销售件数同比"
          />
        </el-form-item>
        <el-form-item label="综合退货率" prop="comprehensiveReturnRate">
          <el-input
            v-model="form.comprehensiveReturnRate"
            placeholder="请输入综合退货率"
          />
        </el-form-item>
        <el-form-item
          label="综合退货率环比"
          prop="comprehensiveReturnRateChain"
        >
          <el-input
            v-model="form.comprehensiveReturnRateChain"
            placeholder="请输入综合退货率环比"
          />
        </el-form-item>
        <el-form-item label="综合退货率同比" prop="comprehensiveReturnRateYoy">
          <el-input
            v-model="form.comprehensiveReturnRateYoy"
            placeholder="请输入综合退货率同比"
          />
        </el-form-item>
        <el-form-item label="折扣率" prop="discountRate">
          <el-input v-model="form.discountRate" placeholder="请输入折扣率" />
        </el-form-item>
        <el-form-item label="折扣率环比" prop="discountRateChain">
          <el-input
            v-model="form.discountRateChain"
            placeholder="请输入折扣率环比"
          />
        </el-form-item>
        <el-form-item label="折扣率同比" prop="discountRateYoy">
          <el-input
            v-model="form.discountRateYoy"
            placeholder="请输入折扣率同比"
          />
        </el-form-item>
        <el-form-item label="主推款销售额占比" prop="operationMainAmountRatio">
          <el-input
            v-model="form.operationMainAmountRatio"
            placeholder="请输入主推款销售额占比"
          />
        </el-form-item>
        <el-form-item
          label="主推款销售额占比环比"
          prop="operationMainAmountRatioChain"
        >
          <el-input
            v-model="form.operationMainAmountRatioChain"
            placeholder="请输入主推款销售额占比环比"
          />
        </el-form-item>
        <el-form-item
          label="主推款销售额占比同比"
          prop="operationMainAmountRatioYoy"
        >
          <el-input
            v-model="form.operationMainAmountRatioYoy"
            placeholder="请输入主推款销售额占比同比"
          />
        </el-form-item>
        <el-form-item label="亏损销售额占比" prop="lossAmountRatio">
          <el-input
            v-model="form.lossAmountRatio"
            placeholder="请输入亏损销售额占比"
          />
        </el-form-item>
        <el-form-item label="亏损销售额占比环比" prop="lossAmountRatioChain">
          <el-input
            v-model="form.lossAmountRatioChain"
            placeholder="请输入亏损销售额占比环比"
          />
        </el-form-item>
        <el-form-item label="亏损销售额占比同比" prop="lossAmountRatioYoy">
          <el-input
            v-model="form.lossAmountRatioYoy"
            placeholder="请输入亏损销售额占比同比"
          />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listBiMonitoringMiddleDwd,
  getBiMonitoringMiddleDwd,
  delBiMonitoringMiddleDwd,
  addBiMonitoringMiddleDwd,
  updateBiMonitoringMiddleDwd,
  synData,
} from "@/api/sale/BiMonitoringMiddleDwd";

export default {
  name: "BiMonitoringMiddleDwd",
  data() {
    return {
      isOpen: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 商品监控缓存表格数据
      BiMonitoringMiddleDwdList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requestCode: null,
        salesAmount: null,
        salesAmountChain: null,
        salesAmountYoy: null,
        returnNumber: null,
        returnNumberChain: null,
        returnNumberYoy: null,
        returnAmount: null,
        returnAmountChain: null,
        returnAmountYoy: null,
        salesNumber: null,
        salesNumberChain: null,
        salesNumberYoy: null,
        comprehensiveReturnRate: null,
        comprehensiveReturnRateChain: null,
        comprehensiveReturnRateYoy: null,
        discountRate: null,
        discountRateChain: null,
        discountRateYoy: null,
        operationMainAmountRatio: null,
        operationMainAmountRatioChain: null,
        operationMainAmountRatioYoy: null,
        lossAmountRatio: null,
        lossAmountRatioChain: null,
        lossAmountRatioYoy: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        requestCode: [
          { required: true, message: "请求编码不能为空", trigger: "blur" },
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    // 打开加载层
    openLoading() {
      this.$modal.loading("正在同步数据，请稍候！");
    },
    handlesynData() {
      this.openLoading();
      synData().then((response) => {
        this.$modal.closeLoading();
        if (response.code == 200) {
          this.$modal.msgSuccess("同步成功");
          this.getList();
        }
      });
    },
    //搜索栏展示与收起
    shouqi() {
      // console.log(1);
      this.isOpen = !this.isOpen;
    },
    /** 查询商品监控缓存列表 */
    getList() {
      this.loading = true;
      listBiMonitoringMiddleDwd(this.queryParams).then((response) => {
        this.BiMonitoringMiddleDwdList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        requestCode: null,
        salesAmount: null,
        salesAmountChain: null,
        salesAmountYoy: null,
        returnNumber: null,
        returnNumberChain: null,
        returnNumberYoy: null,
        returnAmount: null,
        returnAmountChain: null,
        returnAmountYoy: null,
        salesNumber: null,
        salesNumberChain: null,
        salesNumberYoy: null,
        comprehensiveReturnRate: null,
        comprehensiveReturnRateChain: null,
        comprehensiveReturnRateYoy: null,
        discountRate: null,
        discountRateChain: null,
        discountRateYoy: null,
        operationMainAmountRatio: null,
        operationMainAmountRatioChain: null,
        operationMainAmountRatioYoy: null,
        lossAmountRatio: null,
        lossAmountRatioChain: null,
        lossAmountRatioYoy: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加商品监控缓存";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getBiMonitoringMiddleDwd(id).then((response) => {
        this.form = response.data;
        this.open = true;
        this.title = "修改商品监控缓存";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          if (this.form.id != null) {
            updateBiMonitoringMiddleDwd(this.form).then((response) => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addBiMonitoringMiddleDwd(this.form).then((response) => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除商品监控缓存编号为"' + ids + '"的数据项？')
        .then(function () {
          return delBiMonitoringMiddleDwd(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "sale/BiMonitoringMiddleDwd/export",
        {
          ...this.queryParams,
        },
        `BiMonitoringMiddleDwd_${new Date().getTime()}.xlsx`
      );
    },
  },
};
</script>
